// JavaScript Document
BingoTouch.init = function () {
    /*清除loginHashMap保存的信息*/
    //BingoTouch.Local.clearHashmap("loginHashMap");
};

BingoTouch.ready = function () {
    var name = BingoTouch.Local.readHashmap("loginHashMap", "username");
    var specSecret = BingoTouch.Local.readHashmap("loginHashMap", "spec_secret");

    //自动登录
    var isAutoLogin = BingoTouch.Local.readHashmap("loginHashMap", "isAutoLogin");
    if (isAutoLogin != null && isAutoLogin != "" && isAutoLogin == "true") {
        //应用专用密码登录
        var param = { 'userName': name, 'password': specSecret, 'get_spec_secret': false, 'get_service_ticket': false };
        BingoTouch.SSO.Login("SPECSECRET", param, function (response) {
            /*登录到后台服务*/
            AfterSSOLogin();
        },
        function (code, desc) {
            BingoTouch.Common.alert(desc);
        });
    }
};
/*登录事件*/
function Login() {
    if ($("#userName").val() == "" || $("#password").val() == "") {
        BingoTouch.Common.alert("用户信息不能为空！");
        return;
    }
    var name = $("#userName").val();
    var pwd = $("#password").val();

    var getServiceTicket = false;
    var getSpecSecret = false;
    if ($("#cbAuto").attr("class") == "BTCheck_ON") {
        getSpecSecret = true;
    }
    
    var param = { 'userName': name, 'password': pwd, 'get_spec_secret': getSpecSecret, 'get_service_ticket': getServiceTicket };
    BingoTouch.SSO.Login('PASSWORD', param, function (ret) {

        //自动登录
        if ($("#cbAuto").attr("class") == "BTCheck_ON") {
            BingoTouch.Local.saveHashmap("loginHashMap", "isRemember", "true");
            BingoTouch.Local.saveHashmap("loginHashMap", "username", $("#userName").val() + "@bingosoft.net");
            BingoTouch.Local.saveHashmap("loginHashMap", "token", ret.token.id);
            BingoTouch.Local.saveHashmap("loginHashMap", "spec_secret", ret.appSpecifiedSecret.id);
            BingoTouch.Local.saveHashmap("loginHashMap", "spec_secret_expires", ret.appSpecifiedSecret.expires);
            BingoTouch.Local.saveHashmap("loginHashMap", "loginId", ret.principal.id);
            BingoTouch.Local.saveHashmap("loginHashMap", "isAutoLogin", "true");

        }
        BingoTouch.Local.saveHashmap("loginHashMap", "obj", BingoTouch.Common.json2string(ret));
        AfterSSOLogin();

    }, function (code, desc) {
        BingoTouch.Common.alert(desc);
    });
}

function AfterSSOLogin() {
    // login to other App Server
    //LoginToAppServer(appServerBase, null, {});//appServerBase is base directory of the web site
    //LoginToAppServer("http://tproject.bingosoft.net:88/EVMAppService/", "home.html", { });
    //LoginToAppServer("http://developer.bingosoft.net/demo/mobile-server/", "home.html", {});
    //LoginToAppServer("http://developer.bingosoft.net/demo/mobile-server-dotnet/", "home.html", {});
    ////login to my App Server
    LoginToAppServer(context_path, "index.html", {});
}

function LoginToAppServer(serverPath, redictUrl, data) {
    /*获取登录后的回调对象*/
    var loginObj = BingoTouch.Local.readHashmap("loginHashMap", "obj");

    _path = serverPath;
    _data= data;
    _redictUrl = redictUrl;
    var obj = {
        _path: serverPath,
        _data: data,
        _redictUrl: redictUrl,
        success: function (ret) {
            BingoTouch.Common.WebRequest(this._path + "ssoclient/login/serviceticket?openid.ex.service_ticket=" + ret, "POST", "", "", false,
            function (response) {
                if (this._redictUrl != "") {
                    if (this._data == "") {
                        BingoTouch.WebView.load(this._redictUrl);
                    } else {
                        BingoTouch.WebView.load(this._redictUrl, BingoTouch.Common.json2string(this._data));
                    }
                }
            },
            function (code, desc) {
                BingoTouch.Common.alert("请求ServiceTicket失败!" + desc);
            });
        },
        error: function (code, desc) {
            BingoTouch.Common.alert("请求ServiceTicket失败!" + desc);
        }
    };
    BingoTouch.SSO.ServiceTicket(BingoTouch.Common.string2json(loginObj), obj.success,obj.error);
}